package com.java.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.java.entity.SuggestInfo;
import com.java.entity.event.SuggestInfoEvent;
import com.java.mapper.SuggestInfoMapper;
import com.java.service.SuggestService;
import com.java.util.MyTools;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;

@Service
public class SuggestServiceImpl implements SuggestService {

	@Autowired private SuggestInfoMapper suggestMapper;
	
	@Override
	public BaseRespone saveSuggest(String suggestId, String text, int show) {
		int row = 0;
		SuggestInfo info = suggestMapper.selectByKey(suggestId);
		if(null == info) {
			info = new SuggestInfo();
			info.setSuggestId(MyTools.getUUID())
			.setSuggestText(text)
			.setSuggestShow(show)
			.setSuggestTime(new Date());
			row = suggestMapper.insertSelective(info);
		}else {
			info.setSuggestText(text)
			.setSuggestShow(show);
			row = suggestMapper.updateSelective(info);
		}
		return BaseRespone.result(row, "已保存");
	}

	@Override
	public TableRespone getSuggestList(int page, int size) {
		List<SuggestInfoEvent> list = suggestMapper.selectAll(null, page, size);
		long ll = suggestMapper.selectAllCount(null);
		return new TableRespone(list, ll);
	}

	@Override
	public BaseRespone delSuggest(String suggestId) {
		int row = suggestMapper.deleteByKey(suggestId);
		return BaseRespone.result(row, "已删除");
	}

	@Override
	public SuggestInfoEvent getSuggestOne() {
		return suggestMapper.selectRand();
	}

}
